#include <iostream>
// 基类
class Animal {
public:
    virtual void Speak() { std::cout << "Animal sound" << std::endl; }
    virtual ~Animal() {}  // 必须有虚函数，才可以使用 dynamic_cast
    virtual void Speak2() {}
    void Speak3() {}
};
// 派生类
class Dog : public Animal {
public:
    void Speak() override { std::cout << "Woof!" << std::endl; }
    void Fetch() { std::cout << "Dog fetches a stick." << std::endl; }
};
// 另一个派生类
class Cat : public Animal {
public:
    void Speak() override { std::cout << "Meow!" << std::endl; }
};
int main() {
    Animal* a0 = new Animal();
    (void)a0;
    Animal* a1 = new Dog();  // 实际是 Dog
    Animal* a2 = new Cat();  // 实际是 Cat
    // 尝试将 Animal* 转换为 Dog*
    if (Dog* d = dynamic_cast<Dog*>(a1)) {
        std::cout << "a1 is a Dog: ";
        d->Speak();
        d->Fetch();
    } else {
        std::cout << "a1 is NOT a Dog." << std::endl;
    }
    // 再次尝试（失败情况）
    if (Dog* d = dynamic_cast<Dog*>(a2)) {
        std::cout << "a2 is a Dog: ";
        d->Speak();
    } else {
        std::cout << "a2 is NOT a Dog." << std::endl;
    }
    delete a1;
    delete a2;
    return 0;
}
